package com.portico.dept.solarch

import grails.plugins.springsecurity.Secured

@Secured(["ROLE_SA_ADMIN", "ROLE_SA_USER"])
class ProjectController {
  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
  def scaffold = Project
  def index = {
    redirect(action: "list", params: params)
  }
  def list = {
    params.max = Math.min(params.max ? params.int('max') : 10, 100)
    [projectInstanceList: Project.list(params), projectInstanceTotal: Project.count()]
  }

  def create = {
    def projectInstance = new Project()
    projectInstance.properties = params
    return [projectInstance: projectInstance]
  }

  def save = {
    def projectInstance = new Project(params)
    if (projectInstance.save(flush: true)) {
      flash.message = "${message(code: 'default.created.message', args: [message(code: 'project.label', default: 'Project'), projectInstance.id])}"
      redirect(action: "show", id: projectInstance.id)
    }
    else {
      render(view: "create", model: [projectInstance: projectInstance])
    }
  }

  def show = {
    def projectInstance = Project.get(params.id)
    if (!projectInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'project.label', default: 'Project'), params.id])}"
      redirect(action: "list")
    }
    else {
      [projectInstance: projectInstance]
    }
  }

  def master = {
    def projectInstance = Project.get(params.id)
    if (!projectInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'project.label', default: 'Project'), params.id])}"
      redirect(action: "list")
    }
    else {
      [projectInstance: projectInstance]
    }
    
  }

  def detail = {
    def projectInstance = Project.get(params.id)
    if (!projectInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'project.label', default: 'Project'), params.id])}"
      redirect(action: "list")
    }
    else {
      [projectInstance: projectInstance]
    }
  }

  def edit = {
    def projectInstance = Project.get(params.id)
    if (!projectInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'project.label', default: 'Project'), params.id])}"
      redirect(action: "list")
    }
    else {
      return [projectInstance: projectInstance]
    }
  }

  def update = {
    def projectInstance = Project.get(params.id)
    if (projectInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (projectInstance.version > version) {

          projectInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'project.label', default: 'Project')] as Object[], "Another user has updated this Project while you were editing")
          render(view: "edit", model: [projectInstance: projectInstance])
          return
        }
      }
      projectInstance.properties = params
      if (!projectInstance.hasErrors() && projectInstance.save(flush: true)) {
        flash.message = "${message(code: 'default.updated.message', args: [message(code: 'project.label', default: 'Project'), projectInstance.id])}"
        redirect(action: "show", id: projectInstance.id)
      }
      else {
        render(view: "edit", model: [projectInstance: projectInstance])
      }
    }
    else {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'project.label', default: 'Project'), params.id])}"
      redirect(action: "list")
    }
  }

  def delete = {
    def projectInstance = Project.get(params.id)
    if (projectInstance) {
      try {
        projectInstance.delete(flush: true)
        flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'project.label', default: 'Project'), params.id])}"
        redirect(action: "list")
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'project.label', default: 'Project'), params.id])}"
        redirect(action: "show", id: params.id)
      }
    }
    else {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'project.label', default: 'Project'), params.id])}"
      redirect(action: "list")
    }
  }
}
